package weaver.jobs.jdjs.duss.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author dusongsong
 * @date 2025年06月02日 14:26
 */
public class MryebUtil {
    public static Map<String,String> getMrsjye(String ztgs, String gszh, String rq) {
        RecordSet rs = new RecordSet();
        Map<String,String> resultMap = new HashMap<>();
        rs.executeQuery("select sum(je) from uf_gsztzhzjqkb where sz=0 and ztgs=? and gszh=? and gdrq=?",ztgs,gszh,rq);
        rs.next();
        resultMap.put("srje",Util.null2String(rs.getString(1)));
        rs.executeQuery("select sum(je) from uf_gsztzhzjqkb where sz=1 and ztgs=? and gszh=? and gdrq=?",ztgs,gszh,rq);
        rs.next();
        resultMap.put("zcje",Util.null2String(rs.getString(1)));
        return resultMap;
    }

    public static String getMrsjztye(String ztgs, String gszh, String rq) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select ye from uf_mryeb where ztgs=? and gszh=? and rq=?",ztgs,gszh,rq);
        rs.next();
        return Util.null2String(rs.getString("ye"));
    }

    public static String getMaxDay(String ztgs, String gszh) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select max(rq) from uf_mryeb where ztgs=? and gszh=?",ztgs,gszh);
        rs.next();
        return Util.null2String(rs.getString(1));
    }


    public static List<String> printDatesBetween(String startDateStr, String endDateStr) {
        List<String> dateList = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(startDateStr, formatter);
        LocalDate end = LocalDate.parse(endDateStr, formatter);
        LocalDate currentDate = start;
        while (!currentDate.isAfter(end)) {
            currentDate = currentDate.plusDays(1);
            dateList.add(currentDate.format(formatter));
        }
        return dateList;
    }

    /**
     * @Description: 为零判断
     */
    public static String isZero(String a) {
        if ("".equals(a)) {
            a = "0";
        }
        return a;
    }

    /**
     * @Description: 相减
     */
    public static String bSubtract(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.subtract(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }

    /**
     * @Description: 相加
     */
    public static String bAdd(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        String result;
        try {
            BigDecimal bg1 = new BigDecimal(a);
            BigDecimal bg2 = new BigDecimal(b);
            BigDecimal bd = bg1.add(bg2);
            result = bd.toString();
        } catch (NumberFormatException e) {
            result = "";
        }
        return result;
    }
    /**
     *@Description: 建模权限重构
     */
    public static void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid,id );
    }

    /**
     *@Description: 表单建模数据插入
     */
    public static int insertModelData(String tablename, Map<String, Object> map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        String uuid = UUID.randomUUID().toString();
        int formmodeid = getFormmodeid(tablename);
        String insSql = "insert into " + tablename
                + "(modeuuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, 1, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), formmodeid};
        if (rs.executeUpdate(insSql, params)) {
            dataid = ModeUUID2DataId(tablename,uuid);
            if (dataid > 0) {
                //遍历数据 进行update
                StringBuilder updatesql = new StringBuilder("update " + tablename + " set ");
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql.append(key).append("='").append(map.get(key)).append("',");
                }
                if (updatesql.toString().endsWith(",")) {
                    updatesql = new StringBuilder(updatesql.substring(0, updatesql.length() - 1));
                    updatesql.append(" where id=").append(dataid);
                    rs.executeUpdate(updatesql.toString());
                }
                //进行权限重构
                newModeRight(1, formmodeid, dataid);
            }
        }
        return dataid;
    }

    /**
     *@Description: 获取建模Id
     */
    private static int getFormmodeid(String tablename) {
        RecordSet rs = new RecordSet();
        String sql = "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?";
        rs.executeQuery(sql, tablename);
        rs.next();
        return weaver.general.Util.getIntValue(rs.getString("id"));
    }

    /**
     *@Description: 根据建模表名和UUID获取数据ID 常用于建模的权限重构获取数据ID
     */
    public static int ModeUUID2DataId(String tablename,String uuid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tablename + " where modeuuid =?",uuid);
        rs.next();
        return Util.getIntValue(rs.getString(1));
    }

    public static void main(String[] args) {
        String maxDay = "2025-06-20";
        String yestDate = "2025-06-22";
        System.out.println(DateUtil.compDate(maxDay,yestDate));
        if(DateUtil.compDate(maxDay,yestDate)>0){
            List<String> dateList = MryebUtil.printDatesBetween(maxDay, yestDate);
            for (String datestr : dateList) {
                System.out.println(datestr);
            }
        }
    }

}
